//
//  AEDSubViewController.swift
//  GZMarathon
//
//  Created by apple on 2021/10/30.
//

import UIKit
import AVKit
import SVProgressHUD
class AEDSubViewController: UIViewController , UITableViewDelegate , UITableViewDataSource{
    @IBOutlet weak var topView: UIView!
    
    @IBOutlet weak var bottomBt: UIButton!
    
    @IBOutlet weak var tab: UITableView!
    
    @IBOutlet weak var nameLabel: UILabel!
    
    @IBOutlet weak var idLabel: UILabel!

    @IBOutlet weak var responLabel: UILabel!

    @IBOutlet weak var telLabel: UILabel!

    @IBOutlet weak var locationLabel: UILabel!
    
    
    var type = 0 //0.未开 1.开启

    
    
    var list: GZAEDList?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tab.removeMoreLine()
        bindData()
        
        let tap = UITapGestureRecognizer()
        tap.addTarget(self, action: #selector(tapClick))
        
        topView.addGestureRecognizer(tap)
        
        
    }
    
    @objc func tapClick(){
        
        guard let vc = UIStoryboard(name: "GM", bundle: nil).instantiateViewController(withIdentifier: "MapViewController") as? MapViewController else {
            return
        }
        vc.mapType = 0
        
        vc.list = list
   
        navigationController?.pushViewController(vc, animated: true)
        
    }
    
    
    @IBAction func UseClick(_ sender: UIButton) {
        
        if(type == 1){
            ApiLoadingProvider.request(EBAPI.aedTogglestatus(aed_id: list?.id ?? 0, aed_status: "0"), model:BaseModel.self) { res, va in
                if(va == true){
                    self.type = 0
                    self.bottomBt.setTitle("立即使用", for: .normal)
                    self.bottomBt.backgroundColor = UIColor.init("#2E4BF4")
                    self.tab.reloadData()
                }
            }
        }else{
            ApiLoadingProvider.request(EBAPI.aedTogglestatus(aed_id: list?.id ?? 0, aed_status: "1"), model:BaseModel.self) { res, va in
                if(va == true){
                    self.type = 1
                    self.bottomBt.setTitle("停止使用", for: .normal)
                    self.bottomBt.backgroundColor = UIColor.init("#E93939")
                    self.tab.reloadData()
                }
            }
        }
        
        
    }
    

    @IBAction func clickBackBtn(_ sender: UIButton) {
        
        navigationController?.popViewController(animated: true)
        
    }
    func bindData(){
        nameLabel.text = list?.aed_name
        idLabel.text = "\(list?.id ?? 0)"
        responLabel.text = list?.aed_contacts
        telLabel.text = list?.aed_contactnum
        locationLabel.text = list!.province_name + list!.city_name + list!.region_name + list!.aed_address
        
        if(type == 1){
            bottomBt.setTitle("停止使用", for: .normal)
            bottomBt.backgroundColor = UIColor.init("#E93939")
        }

    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 288
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if(type == 0){
            return 1
        }else{
            return 1
        }
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tab.dequeueReusableCell(withIdentifier: "AEDSubTableViewCell", for: indexPath) as! AEDSubTableViewCell
        
        if(indexPath.row == 0){
            cell.titileLabel.text = "指导视频"
            cell.playBtn.isHidden = false
            cell.callBack = {[weak self] in
                self?.playVideo()
            }
        }else{
            cell.titileLabel.text = "心电图"
            cell.playBtn.isHidden = true
        }
        return cell
    }
    
    func playVideo() {
        guard let url = URL(string: list!.aed_videourl) else{
            SVProgressHUD.showError(withStatus: "暂无视频链接")
            return
        }
        let playerVC = AVPlayerViewController()
        let player = AVPlayer(playerItem: AVPlayerItem(url: url))
        playerVC.player = player
        player.play()
        self.present(playerVC, animated: true, completion: nil)
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
}


